package drds.configuration.db.table_mapping;

import java.util.HashMap;
import java.util.Map;

/**
 * 保存模型和表之间的映射关系
 */
public class ModelClassToTableInfoMap {

    public static final Map<Class<? extends Model<?>>, TableInfo> modelClassToTableInfoMap = new HashMap<Class<? extends Model<?>>, TableInfo>();

    private ModelClassToTableInfoMap() {
    }

    public static void addTableInfo(Class<? extends Model<?>> modelClass, TableInfo tableInfo) {
        if (modelClassToTableInfoMap.containsKey(modelClass)) {
            throw new IllegalArgumentException("modelClass:" + modelClass + "重复");
        }
        modelClassToTableInfoMap.put(modelClass, tableInfo);
    }

    public static TableInfo getTableInfo(Class<? extends Model> modelClass) {
        if (modelClassToTableInfoMap == null) {
            throw new NullPointerException("modelAndTableInfoMap为空");
        }
        TableInfo tableInfo = modelClassToTableInfoMap.get(modelClass);
        if (tableInfo == null) {
            throw new NullPointerException("ModelAndTableInfoMap 中不存在 模型:" + modelClass.getName() + " 与之对应的关系映射");
        }
        return tableInfo;
    }

}
